package recursos;
import java.sql.*;
public class conexion {
    public static Connection abrir() {
        String bd="jdbc:postgresql://127.0.0.1:5432/hojavirtual";
        String usr="postgres";
        String psw="1234";
        try {
            Class.forName("org.postgresql.Driver"); 
            Connection con = DriverManager.getConnection(bd,usr,psw);
            return con;
        }
        catch(Exception err) {
            return null;
        }
    }
    
    public static ResultSet consultar(String cadsql) {
        try {
            Connection con = abrir();
            Statement sql;
            ResultSet datos;
            if (con!=null) {
                sql = con.createStatement();
                datos = sql.executeQuery(cadsql);
                return datos;
            }
            return null;
        }
        catch(Exception err) {
            return null;
        }
    }
    
    public static int actualizar(String cadsql) {
        try {
            Connection con = abrir();
            Statement sql;
            int n=0;
            if (con!=null) {
                sql = con.createStatement();
                n = sql.executeUpdate(cadsql);                
            }
            return n;
        }
        catch(Exception err) {
            return 0;
        }
    }
}